/*
 *    This program is free software; you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation; either version 2 of the License, or
 *    (at your option) any later version.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with this program; if not, write to the Free Software
 *    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 *    2011 year
 *    Author: Michael Khromov aka MikeDM (Mike D'Morto)
 *    E-Mail: mikedmorto@gmail.com
 */

#include "logondialog.h"
#include "ui_logondialog.h"
#include <QDebug>
#include <QCloseEvent>
#include "def.h"
#include <QDesktopWidget>

LogonDialog::LogonDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::LogonDialog)
{
    ui->setupUi(this);
    // окошечко по центру, даже если оконный менеджер противица =))
    this->move(QApplication::desktop()->availableGeometry(this).center()-rect().center());
    this->setWindowTitle(APP_NAME+tr(" - standart logon"));
    this->step=LogonDialog::stepShowquery;
    this->ui->pb_enter->setDefault(true);
    this->ui->pb_enter->setDisabled(true);
    this->ui->pte->setVisible(false);
    this->ui->stackedWidget->setCurrentIndex(0);
    // добавляем кнопку минимизации окна.
        this->setWindowFlags(Qt::WindowMinMaxButtonsHint);

}

LogonDialog::~LogonDialog()
{
    delete ui;
}


void LogonDialog::slotEnter(){
    // юзверь решил попробовать войти. понеслась ...
    this->step=LogonDialog::stepWaitresult;
    this->ui->lbl_info->setText(tr("wait please: password authentication ..."));
    this->ui->pb_retry->hide();
    this->ui->pte->setVisible(false);
    this->ui->stackedWidget->setCurrentIndex(1);
    emit this->sigLoginPass(this->ui->le_login->text(),this->ui->le_pass->text());

}

void LogonDialog::slotError(const QString &errtext){
    // что то пошло не так, тут прилетает текст ошибки который надо показать
    // и дать возможность юзверю вернуться к первому шагу
    this->step=LogonDialog::stepError;
    this->ui->pb_retry->setDefault(true);
    this->ui->lbl_info->setText(tr("Call to techsupport.\nerror text:\n"));
    this->ui->pte->setVisible(true);
    this->ui->pte->setPlainText(errtext);
    this->ui->pb_retry->show();
}


void LogonDialog::slotBtnRetry(){
    // вертаем все в начало
    this->step=LogonDialog::stepShowquery;
    this->ui->pb_enter->setDefault(true);
    this->ui->stackedWidget->setCurrentIndex(0);
}

void LogonDialog::slotTestLogin(){
    // проверяем наличие текста в поле логина если нихера нету то убираем возможность нажать на кнопочку вход
    if(this->ui->le_login->text().count()==0){
        this->ui->pb_enter->setDisabled(true);
    }else{
        this->ui->pb_enter->setDisabled(false);
    }

}

void LogonDialog::logonOK(){
    connect(this,SIGNAL(sigEnd()),this,SLOT(accept()));
    emit this->sigEnd();
}
